<?php
namespace app\admin\controller;

use app\admin\service\Menu as MenuService;
class Menu extends Base
{
    //查看菜单
    public function getMenuList()
    {
        $sMenu = new MenuService();
        $list = $sMenu->getMenuList();
        $this->assign('list',$list);
        return $this->fetch('menu/menu_list');
    }

    //添加菜单
    public function addMenuData()
    {
        $list = config('dic.menu_level');
        $data = [];
        foreach ($list as $key => $item) {
            $data[] = ['data_id' => $key, 'data_name' => $item];
        }
        $this->assign('list',$data);
        return $this->fetch('menu/menu_add');
    }

    //获得上级菜单列表
    public function getParentMenu()
    {
        $menu_level = (int)input('id') - 1;
        $sMenu = new MenuService();
        $list = $sMenu-> getParentMenu($menu_level);
        return $list;
    }

    //保存菜单
    public function saveMenuData()
    {
        $sMenu = new MenuService();
        $id = (int)input('id');
        $list['menu_name'] = input('menu_name');
        $list['menu_level'] = input('menu_level');
        $list['menu_url'] = input('menu_url');
        $list['parent_id'] = input('parent_id');
        $list['sort'] = input('sort');
        $res =  $sMenu->saveMenuList($list,$id);
        if ($res) {
            return $this->apiSuccess('菜单已保存成功');
        } else {
            return $this->apiSuccess('菜单保存失败, 请重试');
        }
    }


    //编辑菜单
    public function editMenuData()
    {
        $list = config('dic.menu_level');
        $data = [];
        foreach ($list as $key => $item) {
            $data[] = ['data_id' => $key, 'data_name' => $item];
        }
        $this->assign('list',$data);
        $id = (int)input('id');
        $sMenu = new MenuService();
        $data = $sMenu->getMenuById($id);
        $this->assign('data', $data);
        return $this->fetch('menu/menu_add');
    }

    // 删除菜单
    public function deleteMenuData()
    {
        $id = input('id');
        $sMenu = new MenuService();
        $res = $sMenu->deleteMenuById($id);
        if ($res) {
            return $this->apiSuccess('菜单已成功删除');
        } else {
            return $this->apiSuccess('菜单删除失败, 请重试');
        }
    }

}